/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [PaperLogicImpl.java]
 * Creation Date        [12-Jul-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_04_other.examApp.dao.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import pkg_04_other.examApp.dao.ExaminationAdapter;
import pkg_04_other.examApp.entity.Paper;
import pkg_04_other.examApp.entity.Question;
import pkg_04_other.examApp.util.ExaminationUtil;

/**
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class PaperLogicImpl extends ExaminationAdapter {

	/** 试卷问题数 */
	private int questionCount = 0;

	/** 全部科目 */
	private static Map<String, String> subjectMap = new HashMap<String, String>();

	/**
	 * 获得试卷问题总数
	 */
	@Override
	public int getCount() {
		// 为了在系统主窗口中获得剩余题数
		getPaper();
		return questionCount;
	}

	/**
	 * 获得考试全部科目
	 */
	@Override
	public List<String> getSubjects() {
		List<String> subjects = new ArrayList<String>();

		try {
			List<String> allSubject = ExaminationUtil
					.readFile("src_03_best_practices/pkg_04_other/examApp/configuration"
							+ File.separator + "subject.cfg");
			for (String string : allSubject) {
				String[] temp = string.split("=");
				subjectMap.put(temp[0], temp[1]);
				subjects.add(temp[0]);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return subjects;
	}

	/**
	 * 取得考试科目试卷
	 */
	@Override
	public Paper getPaper() {
		Paper paper = new Paper();
		// 取得考试科目
		String subjectName = subjectMap.get(super.getSubject());
		System.out.println("考试科目为:" + subjectName);
		try {
			List<String> list = ExaminationUtil
					.readFile("src_03_best_practices/pkg_04_other/examApp/configuration"
							+ File.separator + subjectName);

			int count = 0;
			Question question = null;
			List<String> options = null;

			for (int i = 0; i < list.size(); i++) {
				String temp = list.get(i);
				System.out.println("读取:" + temp);
				if (i % 5 == 0) {
					question = new Question();
					// 添加题目
					question.setQuestion(temp);
					options = new ArrayList<String>();
				} else {
					// 添加候选答案
					// 判断是否为正确答案
					if (temp.startsWith("<T>")) {
						temp = temp.substring(3);
						super.getCorrectAnswers().put((questionCount + 1),
								(char) (65 + count) + "");
					}
					count++;
					// 添加选项
					options.add(temp);
					if (count == 4) {
						// 添加选项到题目
						question.setOptions(options);
						questionCount++;
						// 把题目加入试卷
						paper.getQuestions().put(questionCount, question);
						count = 0;
					}
				}
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
		return paper;
	}

}
